import { _decorator, Component, tween, director, Node, UIOpacity, Label } from 'cc';
const { ccclass, property } = _decorator;
import LevelManager from './managers/LevelManager';
import atkUp from './managers/atkData/atkUp';
import healthUp from './managers/healthData/healthUp';
import speedUp from './managers/speedData/speedUp';

@ccclass('Player')
export class Player extends Component {
    @property({
        tooltip: '英雄的生命值',
    })
    health: number = 330;

    @property({
        tooltip: '英雄的攻击力',
    })
    atk: number = 40;

    speed: number = 1;


    healthSave;
    @property(Node)
    fuhuoNode: Node = null;

    @property(Node)
    monsters: Node = null; 

    @property(Node)
    blackScreenNode: Node = null;
    //攻击力显示
    @property(Node)
    atkView: Node = null;
    //生命值显示
    @property(Node)
    healthView: Node = null;
    //攻击速度显示
    @property(Node)
    speedView: Node = null;

    protected onLoad(): void {
        this.serverPlayerData()
        this.healthSave = this.health;
        // console.log('this.healthSave1 = ', this.healthSave);
        let atkLabel = this.atkView.getComponent(Label)
        atkLabel.string = `攻击力:${this.atk}`;
        let healthLabel = this.healthView.getComponent(Label)
        healthLabel.string = `生命值:${this.health}`;
        let speedLabel = this.speedView.getComponent(Label)
        speedLabel.string = `攻击速度:${this.speed}`;
    }

    //开始就把英雄的生命值存到一个对象中
    start(): void {
        
    }

    //假设从服务器获取到新的数值
    serverPlayerData() {
        //从服务器获取英雄数据
        let data;
        // this.health = data;
        // this.atk = data;
        // this.Gold = data;
    }


    //生命值升级后再次把英雄的生命值更新到对象中
    upLevel() {
        this.healthSave = this.health;

    }

    //玩家阵亡后出现的一系列反应
    dieFun() {
        //循环监听玩家是否死亡
        if (this.health > 0) return;
        const levelManager = LevelManager.getInstance();
        levelManager.backLevel()


        this.fuhuoNode.active = true;

        //回到前面的背景位置
        this.backBgPositionFun()
        //黑屏
        this.blackBgFun()

        //把场景中的monster元素都删了
        this.delMonsterFun()

        //玩家死了就重新开始这一关卡
        director.pause();
    }

    fuHuoFun() {
        console.log('英雄复活');

        // const blackScreenUIOpacity = this.blackScreenNode.getComponent(UIOpacity);
        // 黑屏动画
        // tween(blackScreenUIOpacity)
            // .to(0.1, { opacity: 0 })
            // .start();

        this.health = this.healthSave;
        let healthLabel = this.healthView.getComponent(Label)
        healthLabel.string = `生命值:${this.health}`;
        // console.log('this.healthSave3 = ', this.healthSave);
        // console.log('this.health = ', this.health);

    }

    // 删除场景上所有的怪物
    delMonsterFun() {
        while (this.monsters.children.length > 0) {
            let child = this.monsters.children[0];
            this.monsters.removeChild(child);
            child.destroy();
        }
    }

    blackBgFun() {
        // console.log(this.blackScreenNode);

        // const blackScreenUIOpacity = this.blackScreenNode.getComponent(UIOpacity);
        // // console.log(blackScreenUIOpacity);

        // tween(blackScreenUIOpacity)
        //     .to(0.1, { opacity: 255 }) // 1秒内透明度变为255
        //     .start();
    }

    backBgPositionFun() {

    }

    //点击加攻击力
    addAtkBtn() {
        let atkLabel = this.atkView.getComponent(Label)
        let goldup = atkUp.getInstance();
        let getatk = goldup.getAtk();
        this.atk += getatk;
        atkLabel.string = `攻击力:${this.atk}`;
        
    }
    //点击加生命值
    addHealthBtn() {
        let healthLabel = this.healthView.getComponent(Label)
        let healthup = healthUp.getInstance();
        let gethealth = healthup.addhealth();
        this.health += gethealth;
        this.healthSave += gethealth;
        healthLabel.string = `生命值:${this.health}`;
        console.log('保存的生命值',this.healthSave);
        
        
    }
    //点击加攻击速度
    addSpeedBtn() {
        let speedLabel = this.speedView.getComponent(Label)
        let speedup = speedUp.getInstance();
        let getspeed = speedup.addSpeedFun();
        this.speed += getspeed;
        speedLabel.string = `攻击速度:${this.speed}`;
        
    }
    







    //显示生命值
    viewPlayerHealth() {
        let healthLabel = this.healthView.getComponent(Label)
        healthLabel.string = `生命值:${this.health}`;
        
    }
}

